open a new desktop world on pc objective optimize your application and its input capabilities on samsung dex in addition, implement some features using samsung dex dual mode overview since the launch of galaxy s8 and s8+, samsung galaxy flagship devices have supported the samsung dex feature that extends the functionality of a mobile device into the desktop environment in this code lab, you will learn how to optimize their android apps for the next-gen samsung devices with enhanced interaction, desktop mode capability, and a better app experience one of the advantages of samsung dex is that it does not require any sdks to launch apps in samsung dex however, apps must support mouse functionality and multi-density for better ux in dex mode, apps must adhere to a number of android best practices, such as multi-window feature support desktop mode includes all the advanced multitasking features built in to the samsung dex ui you can learn these through the tips and detailed instructions of this code lab set up your environment you will need the following android studio latest version recommended android sdk 24 or higher any samsung galaxy device that supports samsung dex for pc usb-c to usb-a cable if applicable sample code here is a sample code for you to start coding in this code lab download it and start your learning experience! dex sample code 549 44 kb install samsung dex for pc and open the project file samsung dex for pc is the next feature of samsung dex, giving you an uninterrupted experience between your phone and your computer, which is available for both windows os here, you can share a pc monitor, keyboard, and mouse on dex for pc run installer for dex for pc to install after completing the installation, connect your mobile device to your pc using a usb cable open the downloaded sample code which contains the android project for this code lab the base project provided has the following features textviews to show information edittext r id edittxt to input the name text1 r id txt1 to show the sentence with the name entered at edittext text2 r id txt2 to show the size of window text3 r id txt3 to show the status if dex is enabled or not by default, it’s not visible in samsung dex, it can show some text text4 r id txt4 to show how to change the size of the text via keyboard and mouse interaction buttons and switch to run other apps for example, email and subactivity enable mouse and keyboard interaction support to run your app in samsung dex, there is a minimum requirement if apps block fake touch input in dex mode while allowing touch input, you may not use apps with a mouse or touchpad here, you need to check whether the code for disabling mouse and keyboard interaction support are in the manifest or not <uses-configuration android reqtouchscreen="finger" /> <uses-feature android name="android hardware touchscreen" android required="true" /> notenever declare the touchscreen support to disable the mouse and keyboard interactions enable resizable multi-window support to run in samsung dex, apps should support android multi-window – this enables minimizing, maximizing, and resizing if multi-window is not supported, the app opens in a fixed-size window for your app to have a pc-like experience, this needs to be considered apps should target sdk 24 or higher always check the targetsdkversion is set to 24 or a higher at build gradle file explicitly declare resizeableactivity to true in the manifest file to run in a resizable window <activity … android resizeableactivity="true"> </activity> the app can now run in a freeform or resizable multi-window just as resizeableactivity is declared to true explicitly in manifest handle runtime configuration changes when resizing the window resizing the window causes runtime configuration changes similar to changing an orientation from portrait to landscape display this may result in the application being forced to restart whenever resizing the window the app shows a toast message whenever an app is created therefore, after resizing, apps always show it and the sentence of text1 would be reset to ensure that this does not happen, add a configchanges flag to the activity in the manifest you can get more information from android's guide related to handling configuration changes as well as android's best practices on building a responsive design first, add android configchanges flag to activity in manifest here, all configchanges flags prevent the app from restarting in samsung dex <application … <activity <!-- dex todo the android configchanges="colormode|orientation|screenlayout|screensize|smallestscreensize|keyboardhidden|keyboard"this used if you don't want android to kill and restart you activity when window is resized --> android configchanges="colormode|orientation|screenlayout|screensize|smallestscreensize|keyboardhidden|keyboard" > </activity> </application> adding flags in manifest needs to be implemented because onconfigurationchanged callback method is called whenever configuration changes happen sample application already has the method, named updatewindowsize , to check the size width and height of window and update the textview r id txt1 for showing the size of window here, it calls the updatewindowsize whenever call onconfiguration callback occurs @override public void onconfigurationchanged configuration newconfig { super onconfigurationchanged newconfig ; updatewindowsize ; } after this step, the app did not show the toast message after resizing the window and text1 was not set to default check the status related to samsung dex your app can check whether samsung dex is enabled or not in mobile device it is one of the most useful features of samsung dex if your device supports samsung dex, and it is enabled, you can customize your app for dex mode your app can also check whether it is running in your phone or in dex mode in this case, you can customize your app to work differently depending on the mode it is using here, you can make your app show the status if it is enabled or not and can add a text to show which display mode the app is running in check whether samsung dex is enabled or not to check whether samsung dex is enabled or not, you need to use reflection method check and implement the following code inside the isdexenabled if samsung dex is enabled, it returns the value of true private boolean isdexenabled { object desktopmodemanager = mcontext getapplicationcontext getsystemservice "desktopmode" ; if desktopmodemanager != null { try { method getdesktopmodestatemethod = desktopmodemanager getclass getdeclaredmethod "getdesktopmodestate" ; object desktopmodestate = getdesktopmodestatemethod invoke desktopmodemanager ; class desktopmodestateclass = desktopmodestate getclass ; method getenabledmethod = desktopmodestateclass getdeclaredmethod "getenabled" ; int enabled = int getenabledmethod invoke desktopmodestate ; boolean isenabled = enabled == desktopmodestateclass getdeclaredfield "enabled" getint desktopmodestateclass ; method getdisplaytypemethod = desktopmodestateclass getdeclaredmethod "getdisplaytype" ; int displaytype = int getdisplaytypemethod invoke desktopmodestate ; return isenabled && displaytype == desktopmodestateclass getdeclaredfield "display_type_dual" getint desktopmodestateclass ; } catch nosuchfieldexception | nosuchmethodexception | illegalaccessexception | invocationtargetexception e { // device does not support dex 3 0 return false; } } return false; } check which mode your app is currently running when checking which mode your app is running at, you also need to use reflection method check and implement the following code inside isappcurrentlyrunningindex if your app is running at samsung dex mode, it returns the value of true private boolean isappcurrentlyrunningindex { configuration config = mcontext getresources getconfiguration ; try { class configclass = config getclass ; if configclass getfield "sem_desktop_mode_enabled" getint configclass == configclass getfield "semdesktopmodeenabled" getint config { toast maketext mcontext, "isappcurrentlyrunningindex true", toast length_long show ; return true; } } catch exception ignored { } return false; } change the prompt accordingly to enable samsung dex mode using the return value from above methods, change the sentence prompt for textview3 protected void oncreate bundle savedinstancestate { super oncreate savedinstancestate ; setcontentview r layout activity_main ; if isdexenabled { if isappcurrentlyrunningindex { text3 settext "samsung dex enabled \nnow, your app is running in samsung dex" ; } else { text3 settext "samsung dex enabled \nnow, your app is running in phone" ; } } else { text3 settext "samsung dex not enabled" ; } } implement keyboard features and combination keys if your app supports customized keyboard & mouse interactions aside from basic keyboard interactions, it can improve the user’s experience in this step, you can try to add features to control the size for text4 with the combination of ctrl + mouse-wheel add to check the status ctrl keys that has keyevent keycode_ctrl_left and keyevent keycode_ctrl_right as a keycode which would proceed to check its function when pressed and released @override public boolean onkeydown int keycode, keyevent event { switch keycode { case keyevent keycode_ctrl_left case keyevent keycode_ctrl_right ctrlkeypressed = true; break; } return super onkeydown keycode, event ; } @override public boolean onkeyup int keycode, keyevent event { switch keycode { case keyevent keycode_ctrl_left case keyevent keycode_ctrl_right ctrlkeypressed = false; break; } return super onkeyup keycode, event ; } noteif you want to add more keyboard functionality, use the android guide for handling keyboard actions enable ctrl + mouse-wheel combination to change the size of text @override protected void oncreate bundle savedinstancestate { super oncreate savedinstancestate ; setcontentview r layout activity_main ; text4 setongenericmotionlistener new view ongenericmotionlistener { @override public boolean ongenericmotion view view, motionevent motionevent { int action = motionevent getaction ; if action == motionevent action_scroll && ctrlkeypressed { float vscroll = motionevent getaxisvalue motionevent axis_vscroll ; if vscroll < 0 { //wheel down text4 settextsize typedvalue complex_unit_px, text4 gettextsize -1 ; } else{ //wheel up text4 settextsize typedvalue complex_unit_px, text4 gettextsize +1 ; } } return false; } } ; } run other apps on phone or dex display in dual mode samsung dex dual mode is a new feature of samsung dex starting with galaxy note9 with dex for pc support, samsung dex dual mode has become more useful for example, the app makes an activity or another app launches at a specific display in this step, we implement to execute the email app at a specific display via button in the sample app, an email app can be launched on a specific monitor implement to run an email app via button1 r id btn1 @override protected void oncreate bundle savedinstancestate { super oncreate savedinstancestate ; setcontentview r layout activity_main ; startemail = button findviewbyid r id btn1 ; startemail settext "start e-mail app" ; startemail setonclicklistener this ; } @override public void onclick view view { switch view getid { case r id btn1 intent intent = getpackagemanager getlaunchintentforpackage "com google android gm" ; intent addflags intent flag_activity_new_task ; startactivity intent ; break; } } make it decide which display the app will run using a switch the code snippet below can provide the information for target display // samsung dex display displaymanager dm = displaymanager getsystemservice context display_service ; display[] displays = dm getdisplays "com samsung android hardware display category desktop" ; display targetdisplay = displays[0]; // phone display displaymanager dm = displaymanager getsystemservice context display_service ; display targetdisplay = dm getdisplay display default_display ; the app needs to execute startactivity with the display information to get through activityoptions setlaunchdisplayid api finally, we need to change the code to run the email app @override protected void oncreate bundle savedinstancestate { super oncreate savedinstancestate ; setcontentview r layout activity_main ; startemail = button findviewbyid r id btn1 ; startemail settext "start e-mail app" ; startemail setonclicklistener this ; sw = switch findviewbyid r id switch1 ; sw settext "dex display" ; } @override public void onclick view view { display targetdisplay; displaymanager dm = displaymanager getsystemservice context display_service ; if sw ischecked { display[] displays = dm getdisplays "com samsung android hardware display category desktop" ; targetdisplay = displays[0]; }else { targetdisplay = dm getdisplay display default_display ; } switch view getid { case r id btn1 intent intent = getpackagemanager getlaunchintentforpackage "com google android gm" ; if isdexenabled { activityoptions options = activityoptions makebasic ; options setlaunchdisplayid targetdisplay getdisplayid ; startactivity intent, options tobundle ; } else { startactivity intent ; } break; } } utilize both phone and dex display on dual mode in samsung dex dual mode, your app can run different activities on both the phone display and dex display in this case, you should consider a few things first, you need to set target display like in previous step here, you used the targetdisplay defined previously second, to start an additional activity on multi-window, we need to add intent flag_activity_new_task flag intent intent2 = new intent mainactivity this,subactivity class ; intent2 setflags intent flag_activity_new_task ; lastly, the meta-data should be added at manifest <application> … <meta-data android name="com samsung android multidisplay keep_process_alive" android value="true" /> … </application> samsung dex framework does not allow a process to have tasks on multiple displays and contexts such as activities and services if they have the same displayid for this reason, only one instance of the app can run and the older instance of the app will be closed if a new instance is launched however, some apps, including activity or service, needs multiple instances in certain situations this meta-data keeps an application alive and capable of running multiple instances an app developer has a responsibility to use proper context to make proper uis, since the framework is not concerned about the process, application context, and package context in the sample app, subactivity can run on a specific monitor next, implement to start subactivity on the targetdisplay via button2 r id btn2 protected void oncreate bundle savedinstancestate { super oncreate savedinstancestate ; setcontentview r layout activity_main ; startsubactivity = button findviewbyid r id btn2 ; startsubactivity settext "start \nsubactivity" ; startsubactivity setonclicklistener this ; } case r id btn2 intent intent2 = new intent mainactivity this,subactivity class ; intent2 setflags intent flag_activity_new_task ; if isdexenabled { activityoptions options = activityoptions makebasic ; options setlaunchdisplayid targetdisplay getdisplayid ; startactivity intent2, options tobundle ; } else { startactivity intent2 ; } break; test dual mode to open the email app and subactivity in either dex mode or in your phone, you may toggle the on dex display button in the app when on dex display button is turned off, the email app and activity will be launched in the phone otherwise, it will be opened in dex mode as seen below you're done! congratulations! you have successfully achieved the goal of this code lab now, you can optimize your app to be compatible with dex environment by yourself! keep in mind that you can modify your app to have a desktop-like experience without any restrictions or any sdk for samsung dex if you're having trouble, you may download this file dex complete code 8 60 mb